package com.heima.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.domain.pojo.QcCheckType;
import com.heima.domain.response.Result;
import com.heima.service.CheckTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "检验类型管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/qc/checkType")
public class CheckTypeController {
    @Autowired
    private  CheckTypeService checkTypeService;

    @Operation(summary = "分页查询检验类型", description = "根据条件分页查询检验类型")
    @GetMapping("/page")
    public Result<IPage<QcCheckType>> getCheckTypePage(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") Integer pageSize,
            // 新增：接收前端可能传递的查询条件（无则为 null）
            @Parameter(description = "编码（可选）") @RequestParam(required = false) String code,
            @Parameter(description = "名称（可选）") @RequestParam(required = false) String name) {

        Page<QcCheckType> page = new Page<>(pageNum, pageSize);
        IPage<QcCheckType> resultPage = checkTypeService.queryPage(page, code, name);
        return Result.ok(resultPage);
    }

    @Operation(summary = "新增检验类型", description = "创建新的检验类型")
    @PostMapping
    public Result<Boolean> addCheckType(
            @Parameter(description = "检验类型", required = true) @RequestBody QcCheckType qcCheckType) {

        boolean result = checkTypeService.save(qcCheckType);
        return Result.ok(result);
    }

    @Operation(summary = "查询检验类型", description = "根据ID查询检验类型")
    @GetMapping("/{id}")
    public Result<QcCheckType> getCheckTypeById(
            @Parameter(description = "检验类型ID", required = true) @PathVariable Long id) {

        QcCheckType checkType = checkTypeService.getById(id);
        return Result.ok(checkType);
    }


    @Operation(summary = "修改检验类型", description = "更新检验类型")
    @PutMapping
    public Result<Boolean> updateCheckType(
            @Parameter(description = "检验类型", required = true) @RequestBody QcCheckType qcCheckType) {
        boolean result = checkTypeService.updateById(qcCheckType);
        return Result.ok(result);
    }

    @Operation(summary = "删除检验类型", description = "传入ID或ID集合进行删除")
    @DeleteMapping("/delete")
    public Result<Boolean> deleteCheckTypes(@Parameter(description = "检验类型ID或ID集合") @RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.fail("请传入要删除的ID");
        }

        boolean result = checkTypeService.removeByIds(ids);

        if (result) {
            String msg = ids.size() == 1 ? "删除成功" : "批量删除成功（共" + ids.size() + "条）";
            return Result.ok(true, msg);
        } else {
            return Result.fail("删除失败");
        }
    }


    @Operation(summary = "查询所有检验类型", description = "获取所有检验类型列表（用于下拉框）")
    @GetMapping("/list")
    public Result<List<QcCheckType>> getCheckTypeList() {
        List<QcCheckType> list = checkTypeService.list();
        return Result.ok(list);
    }
}
